package com.guli.aliyunoss.controller;

import com.guli.aliyunoss.service.FileService;
import com.guli.aliyunoss.util.PropertiesConstantUtil;
import com.guli.common.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author XiaoHua666
 * @date 2019/4/19
 */

@RestController
@CrossOrigin
@Api(description = "阿里云文件管理")
@RequestMapping("/admin/oss/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 上传文件
     *
     * @param file 文件
     * @return 统一结果对象
     */
    @ApiOperation(value = "文件上传")
    @PostMapping(value = "upload")
    public R uploadFile(
            @ApiParam(value = "文件", name = "file")
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "host", required = false) String host
    ) {
        if(!StringUtils.isEmpty(host)){
            PropertiesConstantUtil.FILE_HOST = host;
        }
        String fileUrl = fileService.upload(file);
        return R.ok().data("url", fileUrl).message("文件上传成功");

    }

}
